package com.bwie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.domain.Store;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.StoreRequest;
import com.bwie.common.result.Result;
import com.bwie.common.utils.FastUtil;
import com.bwie.mapper.StoreMapper;
import com.bwie.service.StoreService;
import com.bwie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @Author：ShenJiaFeng
 * @Package：com.bwie.service.impl
 * @Project：zg6_week11
 * @name：StoreServiceImpl
 * @Date：2024/4/25 10:30
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store>
        implements StoreService {
    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private FastUtil fastUtil;
    @Override
    public Result<List<Store>> storeList(StoreRequest storeRequest) {
        List<Store> stores = storeMapper.storeList(storeRequest);
        return Result.success(stores,"门店信息");
    }

    @Override
    public Result<Store> addStore(Store store) {
        if (store== null){
            throw new RuntimeException("新增数据不能为空");
        }

        boolean save = this.save(
                Store.builder()
                .storeName(store.getStoreName())
                .storeAddress(store.getStoreAddress())
                .businessTime(new Date())
                .storeDistance(store.getStoreDistance())
                .storeImg(store.getStoreImg())
                .build()
        );

        if (save){
            return Result.success(store,"新增成功");
        }
        return Result.error("新增失败");
    }

    @Override
    public Result fastUpd(MultipartFile multipartFile) {
        String fastUpd= "";
        try {
            fastUpd = fastUtil.upload(multipartFile);
            if (fastUpd.length() <= 0){
                return Result.error("上传失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success("http://124.222.71.202:8888/"+fastUpd,"上传成功");
    }

    @Override
    public Result<List<User>> userList() {
        List<User> list = userService.list();
        return Result.success(list);
    }
}
